package exam;

import java.util.Iterator;
import java.util.LinkedList;

public class T11 {
    private abstract static class Animal {
        protected int id;
    }
    private static class Dog extends Animal {
        public Dog(int id) {
            super.id = id;
        }

        @Override
        public String toString() {
            return "Dog{" +
                    "id=" + id +
                    '}';
        }
    }
    private static class Cat extends Animal {
        public Cat(int id) {
            super.id = id;
        }

        @Override
        public String toString() {
            return "Cat{" +
                    "id=" + id +
                    '}';
        }
    }

    private static class AnimalQueue {
        private final LinkedList<Animal> animals = new LinkedList<>();

        public void add(Animal a) {
            animals.add(a);
        }

        public Animal pollAll() {
            return animals.removeFirst();
        }

        public Dog pollDog() {
            return poll(Dog.class);
        }

        public Cat pollCat() {
            return poll(Cat.class);
        }

        public boolean isEmpty() {
            return animals.isEmpty();
        }

        public boolean isDogEmpty() {
            return animals.isEmpty() || take(Dog.class, false) == null;
        }

        public boolean isCatEmpty() {
            return animals.isEmpty() || take(Cat.class, false) == null;
        }

        /**
         * 取出类型为T的
         * @param tClass
         * @param <T>
         * @return
         */
        private <T> T poll(Class<T> tClass) {
            return take(tClass, true);
        }

        /**
         * 拿出类型为T的
         * @param tClass
         * @param delete 是否要删除
         * @param <T>
         * @return
         */
        private <T> T take(Class<T> tClass, boolean delete) {
            if(animals.isEmpty())
                return null;
            Iterator<Animal> iterator = animals.iterator();
            Animal animal;
            while(iterator.hasNext())
                if(tClass.isInstance(animal = iterator.next())) {
                    if(delete)
                        iterator.remove();
                    return (T) animal;
                }
            return null;
        }
    }

    public static void main(String[] args) {
        AnimalQueue queue = new AnimalQueue();
        queue.add(new Dog(1));
        queue.add(new Cat(1));
        queue.add(new Dog(2));
        queue.add(new Cat(2));
        queue.add(new Dog(3));
        queue.add(new Cat(3));

        System.out.println("pollAll -> " + queue.pollAll());
        System.out.println("pollDog -> " + queue.pollDog());
        System.out.println("pollDog -> " + queue.pollDog());
        System.out.println("isEmpty -> " + queue.isEmpty());
        System.out.println("isDogEmpty -> " + queue.isDogEmpty());
        System.out.println("isCatEmpty -> " + queue.isCatEmpty());

        System.out.println("----------------------");

        System.out.println("pollAll -> " + queue.pollAll());
        System.out.println("pollCat -> " + queue.pollCat());
        System.out.println("pollCat -> " + queue.pollCat());
        System.out.println("isEmpty -> " + queue.isEmpty());
        System.out.println("isDogEmpty -> " + queue.isDogEmpty());
        System.out.println("isCatEmpty -> " + queue.isCatEmpty());
    }
}
